package cn.bsb.tools;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 
 * 
 */
public class DateTimeUtil {

	public static String getPreviousDateByHourString(int hours) {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.HOUR_OF_DAY, -hours);

		String format = "yyyy-MM-dd HH:mm:ss";
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(calendar.getTime());

	}

	public static Date getPreviousDateByHourDate(int hours) {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.HOUR_OF_DAY, -hours);

		return calendar.getTime();

	}

	public static String getPreviousDateByMinute(int minute) {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.MINUTE, -minute);

		String format = "yyyy-MM-dd HH:mm:ss";
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(calendar.getTime());
	}

	public static Date getPreviousDateByMinute2(int minute) {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.MINUTE, -minute);

		return calendar.getTime();
	}

	public static String getDateTime7String() {
		Calendar calendar = Calendar.getInstance();
		String format = "yyyy-MM";
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(calendar.getTime());
	}

	public static String getDateTimeString() {
		Calendar calendar = Calendar.getInstance();
		String format = "yyyy-MM-dd HH:mm:ss";
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(calendar.getTime());
	}

	public static String getDateTime10String() {
		Calendar calendar = Calendar.getInstance();
		String format = "yyyy-MM-dd";
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(calendar.getTime());
	}

	public static String getDateTime14String() {
		Calendar calendar = Calendar.getInstance();
		String format = "yyyyMMddHHmmss";
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(calendar.getTime());
	}

	public static String getPreviousDay(int day) {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DAY_OF_MONTH, -day);
		String format = "yyyy-MM-dd";
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(calendar.getTime());
	}
	public static String getTomorrow10() {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DATE, 1);
		String format = "yyyy-MM-dd";
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(calendar.getTime());
	}
	public static String getDateTimeOffsetDay(int day) {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DATE, day);
		String format = "yyyy-MM-dd";
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(calendar.getTime());
	}
	public static String getMonthLastDay() {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
		String format = "yyyy-MM-dd";
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(calendar.getTime());
	}

	public static String getMonthFirstDay() {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
		String format = "yyyy-MM-dd";
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(calendar.getTime());
	}

	public static String getPrevWeekFirstDay() {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.WEEK_OF_MONTH, -1);
		calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		String format = "yyyy-MM-dd";
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(calendar.getTime());
	}

	public static String getPrevMonthFirstDay(int num) {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.MONTH, -num);
		calendar.set(Calendar.DAY_OF_MONTH,
				calendar.getActualMinimum(calendar.DAY_OF_MONTH));
		String format = "yyyy-MM-dd";
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(calendar.getTime());
	}

	public static String getPrevMonthEndDay(int num) {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.MONTH, -num);
		calendar.set(Calendar.DAY_OF_MONTH,
				calendar.getActualMaximum(calendar.DAY_OF_MONTH));
		String format = "yyyy-MM-dd";
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(calendar.getTime());
	}

	public static String getAfterMonthEndDay(String dateStr, int num) {
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		String str = null;
		Date date = null;
		try {
			date = format.parse(dateStr);
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			calendar.add(Calendar.MONTH, +num);
			calendar.set(Calendar.DAY_OF_MONTH,
					calendar.getActualMaximum(calendar.DAY_OF_MONTH));
			str = format.format(calendar.getTime());

		} catch (ParseException e) {
			e.printStackTrace();
		}
		return str;
	}

	/**
	 * 本周的第一天
	 * @return
	 */
	public static String getTheWeekFirstDay() {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
		String format = "yyyy-MM-dd";
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(calendar.getTime());
	}

	/**
	 * 当前时间之前几个月
	 * @param num之前几个月
	 * @return
	 */
	public static String getPreviousMonth(int num) {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.MONTH, -num);

		String format = "yyyy-MM";
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(calendar.getTime());

	}

	/**
	 * 相隔几天
	 * @param t1
	 * @param t2
	 * @return
	 */
	public static int getBetweenDays(String t1, String t2){
		int betweenDays = 0;
		try {
			DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			Date d1 = format.parse(t1);
			Date d2 = format.parse(t2);
			Calendar c1 = Calendar.getInstance();
			Calendar c2 = Calendar.getInstance();
			c1.setTime(d1);
			c2.setTime(d2);
			// 保证第二个时间一定大于第??个时??
			if (c1.after(c2)) {
				c1 = c2;
				c2.setTime(d1);
			}
			int betweenYears = c2.get(Calendar.YEAR) - c1.get(Calendar.YEAR);
			betweenDays = c2.get(Calendar.DAY_OF_YEAR)
					- c1.get(Calendar.DAY_OF_YEAR);
			for (int i = 0; i < betweenYears; i++) {
				c1.set(Calendar.YEAR, (c1.get(Calendar.YEAR) + 1));
				betweenDays += c1.getMaximum(Calendar.DAY_OF_YEAR);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return betweenDays;
	}
	/**
	 * 两个时间相隔几个月
	 * @param t1时间一
	 * @param t2时间二
	 * @return
	 * @throws ParseException
	 */
	public static int getBetweenMonths(String t1, String t2) throws ParseException {
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		int betweenMonths = 0;
		Date d1 = format.parse(t1);
		Date d2 = format.parse(t2);
		Calendar c1 = Calendar.getInstance();
		Calendar c2 = Calendar.getInstance();
		c1.setTime(d1);
		c2.setTime(d2);
		// 保证第二个时间一定大于第??个时??
		if (c1.after(c2)) {
			c1 = c2;
			c2.setTime(d1);
		}
		int betweenYears = c2.get(Calendar.YEAR) - c1.get(Calendar.YEAR);
		betweenMonths = c2.get(Calendar.MONTH) - c1.get(Calendar.MONTH);
		for (int i = 0; i < betweenYears; i++) {
			c1.set(Calendar.YEAR, (c1.get(Calendar.YEAR) + 1));
			betweenMonths += c1.getMaximum(Calendar.MONTH) + 1;
		}
		return betweenMonths;
	}
	
	/**
	 *  yyyy-MM-dd HH:mm:ss
	 * Date转为字符串
	 * @param date
	 * @return
	 */
	public static String getDateToString(Date date){
		String format = "yyyy-MM-dd HH:mm:ss";
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(date);
	}
	/**
	 * yyyy-MM-dd HH:mm:ss
	 * 字符串转为Date
	 * @param dateStr
	 * @return
	 * @throws ParseException
	 */
	public static Date getStringToDateTime(String dateStr) throws ParseException{
		String format = "yyyy-MM-dd HH:mm:ss";
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.parse(dateStr);
	}

	/**
	 * 当前时间（年-周）
	 * @return
	 */
	public static String getYearAndWeek(){
		Calendar calendar = Calendar.getInstance();
		int week = calendar.get(Calendar.WEEK_OF_YEAR);
		int year = calendar.get(Calendar.YEAR);
		String yw = year + "-" + week;
		return yw;
	}

	public static void main(String[] args) {
		try {
			int m = getBetweenDays("2017-06-20", "2017-06-22");
		Date n = getStringToDateTime("2011-09-28 13:32:17");
		String c=getPreviousMonth(1);
		int i=getBetweenMonths("2017-06-20", "2017-11-19");
			System.out.println(getTheWeekFirstDay());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
